import Vue from 'vue'
import moment from 'moment'
import { getDictMap, getDictArray } from './dict'

// 已从cdn加载
// import 'moment/locale/zh-cn'
moment.locale('zh-cn')

Vue.prototype.getFormDict = function (type) {
  return new Promise((resolve, reject) => {
    const list = []
    getDictArray(type).then(response => {
      const res = response.data
      Object.keys(res).forEach(key => {
        list.push({
          'label': res[key].dictLabel,
          'value': String(res[key].dictValue)
        })
      })
      resolve(list)
    }).catch((err) => {
      reject(err)
    })
  })
}
Vue.prototype.getFilter = function (val, map) {
  let str = ''
  // getDictMap(type).then(res => {
  //   // res为map对象
  //   str = res.get(type)
  // })
  // Object.keys(arr).forEach(key => {
  //   if (String(val) === String(arr[key].dictValue)) {
  //     str = arr[key].dictLabel
  //   }
  // })
  str = map.get(val)
  return str
}
Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

export function creatFilter () {
  const getDictMapArr = ['hospitalTurn', 'patientGender', 'patientMedicareType', 'patientType']
  getDictMapArr.forEach(key => {
    Vue.filter(key + 'Filter', function (type) {
      return type
    })
    getDictMap('ref' + key).then(res => {
      Vue.filter(key + 'Filter', function (type) {
        return res.get(type) || '--'
      })
    })
  })
  getDictMap('refapprovalStatus').then(res => {
    Vue.filter('statusFilter', function (type) {
      return res.get(type) || '--'
    })
  })
  Vue.filter('statusTypeFilter', function (type) {
    let status = ''
    switch (String(type)) {
      case '1':
        status = 'warning'
        break
      case '2':
        status = 'success'
        break
      case '3':
        status = 'error'
        break
      default:
        status = 'default'
        break
    }
    return status
  })
}
